<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_loader.php');

/**
 * @package auxo
 * @subpackage test
 * @version $Id$
 */

class MockFixture {
	public function __construct() {		
	}
	
	public function aMethodWithoutParameters() {
		return 'Paris';
	}
	
	public function aMethodWithParameters($a, $b) {		
		return $a * $b;
	}
	
	public function aMethodWithTypedParameters(array $a, MockFixture $b) {
		return true;		
	}
	
	public function aMethodWithDefaultValueParameter($a='Berlin') {		
		return $a;
	}
	
	public function aMethodWithDefaultConstantParameter($a=false) {		
		return $a;
	}
	
	public function aMethodWithNullParameter($a=null) {
		return $a;
	}
}

/**	
 * The Component Proxy Builder Testcase
 *	
 * @package auxo
 * @subpackage test
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_AOPComponentProxyBuilder_testcase extends PHPUnit_Framework_testcase  {
	/**
	 * Sets up a test enviroment of this testcase.
	 */
	public function setUp() {
		$this->proxy = tx_auxo_AOPComponentProxyBuilder::build('MockFixture', 'ProxyFixture');
	}
	
	/**
	 * Testcase: create a proxy class
	 * 
	 * @test
	 */
	public function testCreateAProxyClass() {
		$this->assertNotEquals(0, strlen($this->proxy));
		$this->assertRegExp('/class ProxyFixture extends MockFixture/', $this->proxy);
	}

	/**
	 * Testcase: Proxy class has a constructor method 
	 * 
	 * @test
	 */
	public function testProxyContainsConstructor() {
		$this->assertRegExp('/public function __construct\(\)/', $this->proxy);
	}	
	
	/**
	 * Testcase: Proxy class has a method with a NULL parameter
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithNULLParameter() {
		$this->assertRegExp('/public function aMethodWithNullParameter\(\$a=NULL\)/', $this->proxy);
	}
	
	/**
	 * Testcase: Proxy class has a method with a default constant parameter
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithDefaultConstantParameter() {
		$this->assertRegExp('/public function aMethodWithDefaultConstantParameter\(\$a=false\)/', $this->proxy);
	}	
		
	/**
	 * Testcase: Proxy class has a method with a default value parameter
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithDefaultValueParameter() {
		$this->assertRegExp('/public function aMethodWithDefaultValueParameter\(\$a=\'Berlin\'\)/', $this->proxy);
	}		
	
	/**
	 * Testcase: Proxy class has a method with typed parameters
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithTypedParameters() {
		$this->assertRegExp('/public function aMethodWithTypedParameters\(array \$a,MockFixture \$b\)/', $this->proxy);
	}	

	/**
	 * Testcase: Proxy class has a method with parameters
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithParameters() {
		$this->assertRegExp('/public function aMethodWithParameters\(\$a,\$b\)/', $this->proxy);
	}
		
	/**
	 * Testcase: Proxy class has a method without parameters
	 * 
	 * @test
	 */
	public function testProxyContainsMethodWithoutParameters() {
		$this->assertRegExp('/public function aMethodWithoutParameters()/', $this->proxy);
	}	
		
	/**
	 * Testcase: Proxy object of a proxy class is instansiated
	 * 
	 * @test
	 */
	public function testProxyIsInstansiated() {	
		$test = tx_auxo_AOPComponentProxyBuilder::build('MockFixture', 'ProxyFixtureForNew');
		eval($test);		
		$this->assertType('ProxyFixtureForNew', new ProxyFixtureForNew());
	}	

	/**
	 * Testcase: Project Object has all methods
	 *
	 * @test
	 */
	public function testProxyObjectHasAllMethods() {
		$test = tx_auxo_AOPComponentProxyBuilder::build('MockFixture', 'ProxyFixtureForMethods');
		eval($test);		
		
		$instance = new ProxyFixtureForMethods();
		$this->assertTrue(method_exists($instance, 'aMethodWithoutParameters'));
		$this->assertTrue(method_exists($instance, 'aMethodWithParameters'));
		$this->assertTrue(method_exists($instance, 'aMethodWithTypedParameters'));
		$this->assertTrue(method_exists($instance, 'aMethodWithDefaultValueParameter'));
		$this->assertTrue(method_exists($instance, 'aMethodWithDefaultConstantParameter'));
		$this->assertTrue(method_exists($instance, 'aMethodWithNullParameter'));
	}
	
	/**
	 * Testcase: Proxy object methods return proper values
	 * 
	 * @test
	 */
	public function testProxyObjectMethodsReturnProperValues() {
		$test = tx_auxo_AOPComponentProxyBuilder::build('MockFixture', 'ProxyFixtureForReturn');
		eval($test);		
		$instance = new ProxyFixtureForReturn();	
		$this->assertEquals('Paris', $instance->aMethodWithoutParameters());
		$this->assertEquals(12, $instance->aMethodWithParameters(3,4));
		$this->assertTrue($instance->aMethodWithTypedParameters(array(), new MockFixture()));
		$this->assertEquals('Berlin', $instance->aMethodWithDefaultValueParameter());	
		$this->assertEquals('Paris', $instance->aMethodWithDefaultValueParameter('Paris'));		
		$this->assertFalse($instance->aMethodWithDefaultConstantParameter());
		$this->assertTrue($instance->aMethodWithDefaultConstantParameter(true));		
		$this->assertNull($instance->aMethodWithNullParameter());
        $this->assertNotNull($instance->aMethodWithNullParameter(new MockFixture));
	}		
	
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
		unset($this->proxy);
	}
}